package com.tender.integration.acl;

import com.tender.common.Result;
import com.tender.constants.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.tender.WebSocketServerApi;
import org.springframework.web.client.RestTemplate;

/**
 * Anti-Corruption Layer
 * WebSocketServer 相关的-"防腐层"
 */
@Slf4j
@Component
@SuppressWarnings("all")
public class WebSocketServerApiACL {

    private final String URI = "/api/ws/server/count/all";

//    @Autowired
//    private WebSocketServerApi webSocketServerApi;

    @Autowired
    private RestTemplate restTemplate;

    public Integer userCount(String applicationName) {
        try {
            /**
             * web socket operation 在调用
             * @see webSocketServerApi 之前，通过 Spring 的 Environment/环境变量 来动态设置 applicationName
             */
            // System.setProperty(Constants.DYNAMIC_APPLICATION_NAME_KEY, applicationName);
            // Result<Integer> result = webSocketServerApi.userCount();
            String url = "http://" + applicationName + URI;
            Result<Integer> result = restTemplate.getForObject(url, Result.class);
            if (null != result && null != result.getData()) {
                return result.getData();
            }
        } catch (Exception e) {
            log.error("【获取ws-server用户数】applicationName: {}, error: ", applicationName, e);
        }

        return 0;
    }

}
